package com.house365.core.bean;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

import com.house365.androidpn.client.dto.NotificationData;
import com.house365.core.util.store.SharedPreferencesDTO;

public class NotificationDataRec extends SharedPreferencesDTO<NotificationDataRec> {
	private static final long serialVersionUID = -829039834277394475L;
	
    /* 内部类，定义了推送消息表名和表中的字段名*/
    public static abstract class PushTable {
        public static final String TABLE_NAME = "push_table";
        public static final String COLUMN_NAME_PUSH_ID = "pushid";
        public static final String COLUMN_NAME_CITY = "city";
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_MESSAGE = "message";
        public static final String COLUMN_NAME_URI = "uri";
        public static final String COLUMN_NAME_ALREADY_READ = "read";
        public static final String COLUMN_NAME_TIMESTAMP = "timestamp";
    }
    private static final String TEXT_TYPE = " TEXT";
    private static final String INTEGER_TYPE = " INTEGER";
    private static final String COMMA_SEP = ",";
    public static final String PUSH_TABLE_CREATE =
            "CREATE TABLE IF NOT EXISTS " + PushTable.TABLE_NAME + " (" +
                "_id " + "INTEGER PRIMARY KEY AUTOINCREMENT, " +//主键，自增长
                PushTable.COLUMN_NAME_PUSH_ID + TEXT_TYPE + COMMA_SEP +
                PushTable.COLUMN_NAME_CITY + TEXT_TYPE + COMMA_SEP +
                PushTable.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
                PushTable.COLUMN_NAME_MESSAGE + TEXT_TYPE + COMMA_SEP +
                PushTable.COLUMN_NAME_URI + TEXT_TYPE + COMMA_SEP +
                PushTable.COLUMN_NAME_ALREADY_READ + INTEGER_TYPE + COMMA_SEP +
                PushTable.COLUMN_NAME_TIMESTAMP + " DATETIME DEFAULT (datetime(CURRENT_TIMESTAMP,'localtime'))" +
        " )";
    public static final String PUSH_TABLE_DELETE =
            "DROP TABLE IF EXISTS " + PushTable.TABLE_NAME;
	private String id;
	private String title;
	private String message;
	private String uri;
	
	/** 
	 * 推送消息是否已经被读过
	 * <p>0  未读
	 * <p>1  读过*/
	private int alreadyRead;
	
	/** 
	 * 接收到推送消息的时间，用于删除过期的推送消息和排序推送消息
	 * <p> 格式"2014-11-12 17:10:07"*/
	private String time;
	
	/** 接收到推送消息时，所在的城市*/
	private String city;
	
    public NotificationDataRec() {
	}

    /** 测试使用 */
    public NotificationDataRec(String id, String title, String message,
            String uri, int alreadyRead, String city) {
        super();
        this.id = id;
        this.title = title;
        this.message = message;
        this.uri = uri;
        this.city = city;
        this.alreadyRead = 0;
    }
    
    public NotificationDataRec(NotificationData notification) {
        super();
        this.id = notification.getId();
        this.title = notification.getTitle();
        this.message = notification.getMessage();
        this.uri = notification.getUri();
    }
    
	public NotificationDataRec(NotificationData notification, String city) {
		super();
		this.id = notification.getId();
		this.title = notification.getTitle();
		this.message = notification.getMessage();
		this.uri = notification.getUri();
		this.city = city;
	}

    public static NotificationDataRec createFromCursor(Cursor cursor) {
        if (cursor == null) {
            return null;
        }
        if (!cursor.moveToNext()) {
            return null;
        }
        NotificationDataRec data = getNotification(cursor);
        return data;
    }
    public static List<NotificationDataRec> createListFromCursor(Cursor cursor) {
        List<NotificationDataRec> list = new ArrayList<NotificationDataRec>();
        if (cursor == null) {
            return list;
        }
        while (cursor.moveToNext()) {
            NotificationDataRec data = getNotification(cursor);
            list.add(data);
        }
        return list;
    }
    
    private static NotificationDataRec getNotification(Cursor cursor) {
        NotificationDataRec data = new NotificationDataRec();
        data.id = cursor.getString(cursor.getColumnIndex(PushTable.COLUMN_NAME_PUSH_ID));
        data.city = cursor.getString(cursor.getColumnIndex(PushTable.COLUMN_NAME_CITY));
        data.title = cursor.getString(cursor.getColumnIndex(PushTable.COLUMN_NAME_TITLE));
        data.message = cursor.getString(cursor.getColumnIndex(PushTable.COLUMN_NAME_MESSAGE));
        data.uri = cursor.getString(cursor.getColumnIndex(PushTable.COLUMN_NAME_URI));
        data.time = cursor.getString(cursor.getColumnIndex(PushTable.COLUMN_NAME_TIMESTAMP));
        data.alreadyRead = cursor.getInt(cursor.getColumnIndex(PushTable.COLUMN_NAME_ALREADY_READ));
        return data;
    }
	
	public ContentValues getContentValues() {
	    ContentValues values = new ContentValues();
	    values.put(PushTable.COLUMN_NAME_PUSH_ID, id);
	    values.put(PushTable.COLUMN_NAME_CITY, city);
	    values.put(PushTable.COLUMN_NAME_TITLE, title);
	    values.put(PushTable.COLUMN_NAME_MESSAGE, message);
	    values.put(PushTable.COLUMN_NAME_URI, uri);
	    values.put(PushTable.COLUMN_NAME_ALREADY_READ, alreadyRead);
	    return values;
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

    public int getAlreadyRead() {
        return alreadyRead;
    }
    
    public void setAlreadyRead(int alreadyRead) {
        this.alreadyRead = alreadyRead;
    }
    
	public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
	public boolean isSame(NotificationDataRec o) {
		return id.equals(o.getId());
	}

}
